package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;


    @GetMapping("/pool")
    public Result poolList(BusinessPoolDTO dto){
        PageResult<Business> pageResult = businessService.poolList(dto);
        return Result.success(pageResult);
    }


    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id){
        businessService.toCustomer(id);
        return Result.success();
    }


    @PutMapping("/back/{id}")
    public Result updateRecoveryStatus(@PathVariable Integer id){
        businessService.updateById(
                Business.builder()
                        .id( id)
                        .status(BusinessStatusConstants.RECOVERY)
                        .updateTime(LocalDateTime.now())
                        .build());
        return Result.success();
    }


    @PutMapping
    public Result updateBusinessInfoById(@RequestBody UpdateBusinessInfoDTO dto){
        businessService.updateBusinessInfoById(dto);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getBusinessInfoById(@PathVariable Integer id){
        BusinessVO business = businessService.getBusinessInfoById(id);
        return Result.success(business);
    }

    @PutMapping("/assign/{businessId}/{userId}")
    public Result updateBusinessById(@PathVariable Integer businessId, @PathVariable Integer userId){
        businessService.updateById(
                Business.builder()
                .id(businessId)
                .status(BusinessStatusConstants.WAIT_FOLLOW_UP)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .build());

        return Result.success();

    }

    @GetMapping
    public Result list(BusinessListDTO dto){

        PageResult<BusinessDO> pageResult = businessService.list(dto);
        return Result.success(pageResult);
    }


    @PostMapping
    public Result addBusiness(@RequestBody BusinessDTO dto){
        businessService.addBusiness(dto);
        return Result.success();
    }

}
